/*
 * @Author: zi.yang
 * @Date: 2022-02-03 22:42:26
 * @LastEditors: zi.yang
 * @LastEditTime: 2022-02-08 23:17:44
 * @Description: In User Settings Edit
 * @FilePath: \api-generator\app\model\user.js
 */
'use strict';
const Encryption = require('../utils/encryption');

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const UserSchema = new Schema({
    username: {
      type: String,
      unique: true,
      required: true,
    },
    salt: {
      type: String,
      unique: false,
      required: false,
    },
    password: {
      type: String,
      unique: false,
      required: true,
      set(val) {
        this.salt = Math.random().toString(36).substr(2);
        return Encryption.$pwd(val, this.salt);
      },
    },
  });

  return mongoose.model('User', UserSchema);
};
